<%@page import="DB.DBProvider"%>
<%@page import="MianCalPak.Homeservlet"%>

<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="java.util.*, java.io.*,java.sql.ResultSet" %>
<% 
   String user = (String)request.getSession().getAttribute("username");
   String admin = (String)request.getSession().getAttribute("administrator");

   String title = (String)request.getParameter("title");

   if ( title == null)
   {
	   response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
	   return; 
   }
   Properties p = new Properties();
   p.load(getServletContext().getResourceAsStream("/WEB-INF/config"));
   DBProvider provider = new DBProvider(p);
   ResultSet res = provider.courseDetails(title);
   if (!res.next()){
	   response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
	   return;
   }
	
	String description = res.getString("description");
	String credit_points = Integer.toString(res.getInt("number_credit_points"));
	String capacity = Integer.toString(res.getInt("course_capacity"));
	int icapacity = res.getInt("course_capacity");
	String group_number = Integer.toString(res.getInt("group_number"));
	int igroup_number = res.getInt("group_number");
	ArrayList<String> reasons = new ArrayList<String>(); 
	
	ArrayList<Integer> course_starts = new ArrayList<Integer>();
	ArrayList<Integer> course_ends  = new ArrayList<Integer>();
	
	boolean canReg = false;
	int[] registeredUsers = new int[2];
	reasons = Homeservlet.canRegister(provider,user,title,icapacity,igroup_number,registeredUsers);
	if (reasons.isEmpty())
	{
		canReg = true;
	}
	
	 res = provider.getCourseSessions(title); 
	 int k =0;
	 while (res.next())
	 {
		 course_starts.add(k,res.getInt("start_time_slot"));
		 course_ends.add(k,res.getInt("end_time_slot"));
		 k++;
	 } 
	 res.close();
	
	
		  
%>	

<script type="text/javascript">         

$(document).ready(function()
{
	var clickflag = true;

	$('#sesform').hide();
	
	
	$('#Registeruser').click(function(){
		
		var t = "<%=title%>";
		t = encodeURIComponent(t);
		$('#content').load("/Calendars/Homeservlet?op=registercourse&title="+t,function(response, status, xhr) {
			  if (status == "error") {
				    var msg = "Sorry but there was an error: ";
				    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
				  }
				});

			
	});
	
	
	$('#addses').click(function(){
			
			if(clickflag)
			{
				clickflag = false;
				$('#sesform').show();
				
			}
			else
			{
				   var startend =new Array();
				   var flag = false;
				   
				  $('input:[class^="num"]').each(function(index) {
					   
					   for(var k=0 ; k < $(this).val().length; ++k)
						{
				            
							var new_key1 = $(this).val()[k]; 
						    if((new_key1 < "0") || (new_key1 > "9"))
						    {
						    	alert($(this).attr("name")+" can contain only numbers");
						    	flag = true;
								return false;
								
								
						 	}
						}
					    
				   });
				   
				   $('.Sessiontr').each(function() 
				   {
					  
					   
					   		   $(this).children().each(function(index) 
							   {
								   startend[index] =  $(this).children().val();
								   
							   });
							   
							   if(startend[0] == "")
								{
								   return false;
								}
							   if(startend[1] == "")
								{
								   return false;
								}
							   
							   startend[0] = parseInt(startend[0]);
							   startend[1] = parseInt(startend[1]);
							   if(startend[0] > startend[1] )
								{
								   alert("a session can't end befor it start");
								   flag = true;
								   return false;
								}
							   
							   if((startend[0] < 1) || (startend[0] > 70) || (startend[1] < 1) || (startend[1] > 70) )
								{
								   alert("session's start and end can be numbers between 1-70");
								   flag = true;
								   return false;
								}
							   
							   
							   if((startend[1]- startend[0]) > 3) 
							   {
								   alert("session is a group of 1-4 consecutive time slots");
								   flag = true;
								   return false;
							   }
							   
							   var notsameday = true;
							   if(((startend[0] > 0)&&(startend[0] < 11))&&((startend[1] > 0) && (startend[1] < 11)))
							   {
								   notsameday = false;
							   }
							   if(((startend[0] > 10)&&(startend[0] < 21))&&((startend[1] > 10) && (startend[1] < 21)))
							   {
								   notsameday = false;
							   }
							   if(((startend[0] > 20)&&(startend[0] < 31))&&((startend[1] > 20) && (startend[1] < 31)))
							   {
								   notsameday = false;
							   }
							   if(((startend[0] > 30)&&(startend[0] < 41))&&((startend[1] > 30) && (startend[1] < 41)))
							   {
								   notsameday = false;
							   }
							   if(((startend[0] > 40)&&(startend[0] < 51))&&((startend[1] > 40) && (startend[1] < 51)))
							   {
								   notsameday = false;
							   }
							   if(((startend[0] > 50)&&(startend[0] < 61))&&((startend[1] > 50) && (startend[1] < 61)))
							   {
								   notsameday = false;
							   }
							   if(((startend[0] > 60)&&(startend[0] < 71))&&((startend[1] > 60) && (startend[1] < 71)))
							   {
								   notsameday = false;
							    }
							   
							   
							  if(notsameday)
								{
								   alert("a session need begin and end in the same day!");
								   flag = true;
								   return false;
								}
							   
							   
					});
					 if(flag == true)
					{ 
						  alert("please fix your input");
						  flag = false;
						  return false;
						 
					}

					var t = "<%=title%>";
					t = encodeURIComponent(t);
					$('#content').load("/Calendars/Homeservlet?op=addses&title="+t+
										"&start="+startend[0]+"&end="+startend[1],function(response, status, xhr) {
						  if (status == "error") {
							    var msg = "Sorry but there was an error: ";
							    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
							  }
							});
		
			         return false; 
				
				
				
				
			}
			
	});
	
	
	
	
		    
});
	   
</script>

	
	<div id="content" class="post"">
	 <div id="profileDiv">
		<h1 class="title">Course page</h1>
		<br/>
		<table>
			<tr > 
				<td align="left" width="40%"><h3>Course title: </h3></td>
				<td><%=title%></td>
			</tr>
			<tr>
				<td></td>
				<td></td>
			</tr>
			<tr>
				<td align="left" width="40%"><h3>Description: </h3></td>
					<td></td>
			</tr>
			<tr>
				<td></td>
				<td><%=description%></td>
			</tr>
			<tr>
				<td align="left" width="40%"><h3>Credit points: </h3></td>
					<td><%=credit_points%></td>
			</tr>
			<tr>
				<td></td>
				<td></td>
			</tr>
			<tr>
				<td align="left" width="40%"><h3>Capacity: </h3></td>
					<td><%=capacity%></td>
			</tr>
			<tr>
				<td></td>
				<td></td>
			</tr>
			<tr>
				<td align="left" width="40%"><h3>Available: </h3></td>
					<td><%=Integer.toString(icapacity-registeredUsers[0])%></td>
			</tr>
			<tr>
				<td></td>
				<td></td>
			</tr>
			<tr>
				<td align="left" width="40%"><h3>Group number: </h3></td>
					<td><%=group_number%></td>
			</tr>
			<tr>
				<td></td>
				<td></td>
			</tr>		
		</table>
		<h3 align="left">Registration: </h3>
		<br/>
		<%
		 if(canReg)
		 {
		%>
		<button id="Registeruser">Click Here</button>
		
		<% }
		 else
		 {
		 
			for( String S : reasons )
			{
		%>	 
		   
		   <%=S%>
		   <br/>
				
		<%		
			}
		 }
		%>
		
		<br/>
		<h3 align="left">Sessions: </h3>
		<table>
		<%
		for(int j=0 ; j < course_starts.size() ; j++)
		{
		
		%>
		<tr height="30px"> 
		<td><u>Session <%=Integer.toString(j)%></u></td> 
		<td width="20%"> </td>
		<td>Start : <%=Integer.toString(course_starts.get(j))%></td> 
		<td width="10%"> </td>
		<td>End : <%=Integer.toString(course_ends.get(j))%></td>	
		</tr> 
		<%
		}
		
		%>
		</table>
		
		<% 
		if (admin.equals("true")) {
  		%>
    	<p>Add Sessions: <button id="addses">Click Here</button></p>
		<div id="sesform">
		<table>
		<tr id="Ses" class="Sessiontr">
				<td>start: <input type="text" name="start" class="num" size="15"/></td>
				<td>end: <input type="text" name="end" class="num" size="15"/></td>
		</tr>	
		</table>
		</div>
    	<% } %>

	 </div>
	 </div>
	
